<?php
	require_once("include/jara_fns.php");
	if(isset($_REQUEST["term"])) {
		try {
			jara_page_start("Search results");
			$term = $_REQUEST["term"];
			if(!get_magic_quotes_gpc()) {
				$term = addslashes($term);
			}
			if($term == "") {
				echo "<p>Please enter a search term.</p><p><a href=\"search.php\">Go back</a></p>";
				jara_page_end();
				exit;
			}
			echo "<p><a href=\"feed.php?term=$term\">RSS Feed of Search</a></p>";
			$query = "select * from jara_posts where title like '%$term%' or text like '%$term%'";
			$result = jara_db_query($query);
			$num_rows = $result->num_rows;
			echo "<p><strong>$num_rows</strong> results for <strong>".stripslashes($term)."</strong>.</p>";
			for($i = 0; $i < $num_rows; $i++) {
				$row = $result->fetch_assoc();
				echo "<p>";
				echo "<a href=\"view.php?id=".$row["id"]."\"><strong>".$row["title"]."</strong></a> - <a href=\"profile.php?username=".$row["author"]."\">".$row["author"]."</a> at ".date("jS M Y", $row["time"])."<br />";
				echo nl2br(substr(strip_tags($row["text"]), 0, 200));
				echo "</p>";
			}
			jara_page_end();
			exit;
		}
		catch(JaraDatabaseException $ex) {
?>
<p>
	A database error occured during the process of creating this page.
</p>
<p>
	Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
	Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
</p>
<?php
			jara_page_end();
			exit;
		}
		catch(JaraGeneralException $ex) {
?>
<p>
	A general error occured during the process of creating this page.
</p>
<p>
	Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
</p>
<?php
			jara_page_end();
			exit;
		}
	}
	jara_page_start("Search");
?>
<p>
	On this page, you can search through blog posts.
</p>
<form action="search.php" method="post">
	<p>
		Search term: <input type="text" name="term" id="term" /><input type="submit" value="Go" />
	</p>
</form>
<?php
	jara_page_end();
?>